package edu.cs346.group5.ui.loginpages;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import edu.cs346.group5.datastores.User;
import edu.cs346.group5.logic.Login;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;


public class forgot_second extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JButton btnSubmit;
	private JButton btnBack;
	private JButton btnQuit;
	private JLabel lblNewLabel;
	private HashMap<String, Object> user;
	private JLabel lblNewPassword;
	private JTextField txtNewPS;
	private JButton btnSubmitNewPS;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	public forgot_second() {
		setTitle("Forgot Password");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][grow][grow][][][][][][][]", "[][][][][][][][][][]"));
		
		lblNewLabel = new JLabel("Security Question");
		contentPane.add(lblNewLabel, "cell 0 0 10 1");
		
		textField = new JTextField("Answer");
		contentPane.add(textField, "cell 1 1 9 1,growx");
		textField.setColumns(10);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Boolean check = user.get("secret_answer").toString().equals(textField.getText());
				if(check)
				{
					//do something here, not sure what we wanted
					lblNewPassword.setVisible(true);
					btnSubmitNewPS.setVisible(true);
					txtNewPS.setVisible(true);
				}
				else{
					JOptionPane.showMessageDialog(null, "That is not the correct answer.", "Notice", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		contentPane.add(btnSubmit, "cell 9 2");
		
		btnBack = new JButton("Back");
		btnBack.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				forgot x = new forgot();
				x.setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnBack, "cell 9 3");
		
		btnQuit = new JButton("Quit");
		btnQuit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		contentPane.add(btnQuit, "cell 9 4");
		
		lblNewPassword = new JLabel("New Password:");
		lblNewPassword.setVisible(false);
		contentPane.add(lblNewPassword, "cell 2 7 2 1"); 
		
		txtNewPS = new JPasswordField();
		contentPane.add(txtNewPS, "cell 2 8 8 1,growx");
		txtNewPS.setVisible(false);
		txtNewPS.setColumns(10);
		
		btnSubmitNewPS = new JButton("Submit");
		btnSubmitNewPS.setVisible(false);
		contentPane.add(btnSubmitNewPS, "cell 9 9");
		btnSubmitNewPS.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Login.SetNewPassword(txtNewPS.getText(), (int)user.get("id"));
				login x = new login();
				x.setVisible(true);
				JOptionPane.showMessageDialog(null, "Password has been changed.", "Notice", JOptionPane.INFORMATION_MESSAGE);
				dispose();
			}
		});
	}
	
	public void update_page(HashMap<String, Object> user)
	{
		this.user = user;
		lblNewLabel.setText(Login.GetSecurityQuestion((int)user.get("secret_question_id")));
	}

}
